<template>
  <div id="videolist" class="page-full-screen">
    <div>
      <el-button @click="addTit('新增')">新增</el-button>
      <!--<el-button @click="addTit('屏蔽')">屏蔽</el-button>-->
      <!--<el-button @click="addTit('解除屏蔽')">解除屏蔽</el-button>-->
      <el-button @click="searchshow = !searchshow">搜索&nbsp;<i class="iconfont icon-zhankai" :class="{active: searchshow === true}" style="font-size: 12px"></i>
      </el-button>
    </div>
    <el-collapse-transition>
      <div v-show="searchshow">
        <div class="main-box">
          <span>标题</span>
          <el-input
            placeholder="请输入关键字"
            size="mini"
            prefix-icon="el-icon-search"
            v-model="searchmsg.title">
          </el-input>
        </div>
        <div class="main-box">
          <span>分类名</span>
          <el-select v-model="videocateaddS" size="mini" placeholder="请选择">
            <el-option
              v-for="item in videocateadd"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
          <el-select v-model="searchmsg.category_id" size="mini" placeholder="请选择" v-if="videocateaddS !== '' && videocateaddS !== 0">
            <el-option
              v-for="item in allCate"
              :key="item.id"
              :label="item.alias"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="main-box">
          <span>状态</span>
          <el-radio-group v-model="searchmsg.enabled">
            <el-radio label="">全部</el-radio>
            <el-radio label="1">可用</el-radio>
            <el-radio label="0">不可用</el-radio>
          </el-radio-group>
        </div>
        <div class="main-box">
          <span>年份</span>
          <el-select v-model="searchmsg.year" size="mini" placeholder="请选择">
            <el-option
              v-for="(item,index) in yearcates"
              :key="item.index"
              :label="item.alias"
              :value="item.key">
            </el-option>
          </el-select>
          <span style="margin-left: 20px"><el-button size="mini" @click="sureSearch">确定</el-button></span>
        </div>
      </div>
    </el-collapse-transition>
    <msg-list :tableData="tableData"
              :title="title"
              :btntype="btntype"
              :pagesall="parseInt(pagesall)"
              :pages="parseInt(page)"
              @getselect="getselect"
              @changepage="changepage1"
              @getid="getid1"></msg-list>
    <div class="layer" v-show="showimg">
      <div class="mask" @click="showimg = false"></div>
      <div class="imgcontent">
        <img :src="getmsg.img" alt="">
      </div>
    </div>
    <div class="layer" v-show="seecontent">
      <div class="mask" @click="seecontent = false"></div>
      <div class="content">
        <span class="close" @click="seecontent = false"><i class="iconfont icon-untitled94"></i></span>
        <p class="title">{{addtitle}}视频</p>
        <div class="inputcl">
          <span class="layer-list-title">标题:</span>
          <span><el-input v-model="addvideo.title" placeholder="请输入标题" size="mini"></el-input></span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">作者:</span>
          <span><el-input v-model="addvideo.author" placeholder="请输入作者ID" size="mini"></el-input></span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">视频ID:</span>
          <span><el-input v-model="addvideo.qvid" placeholder="请输入视频ID" size="mini"></el-input></span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">年份:</span>
          <span>
            <el-select v-model="addvideo.year" size="mini" placeholder="请选择">
            <el-option
              v-for="(item,index) in yearcates"
              :key="item.index"
              :label="item.alias"
              :value="item.key">
            </el-option>
          </el-select>
          </span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">分类名:</span>
          <span>
            <el-select v-model="videocateaddV" size="mini" placeholder="请选择视频主题" @change="videocateaddC">
              <el-option
                v-for="item in videocateadd"
                :key="item.id"
                :label="item.title"
                :value="item.id">
              </el-option>
            </el-select>
            <el-select v-model="addvideo.category_id" size="mini" placeholder="请选择" v-if="videocateaddV !== '' && videocateaddV !== 0">
              <el-option
                v-for="item in allCate"
                :key="item.id"
                :label="item.alias"
                :value="item.id">
              </el-option>
            </el-select>
          </span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <!--<span class="layer-list-title img-list">内容:</span>-->
          <span class="imgupload main-cctv">
            <editor ref="editor"></editor>
          </span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">权限:</span>
          <span>
            <el-select v-model="addvideo.view_group" size="mini" placeholder="请选择">
              <el-option
                v-for="item in videocateadd1"
                :key="item.key"
                :label="item.alias"
                :value="item.key">
              </el-option>
            </el-select>
          </span>
        </div>
        <div class="clear"></div>
        <div class="inputcl">
          <span class="layer-list-title">状态:</span>
          <span>
            <el-radio-group v-model="enabled">
              <el-radio :label="0">可用</el-radio>
              <el-radio :label="1">不可用</el-radio>
            </el-radio-group>
          </span>
        </div>
        <div class="clear"></div>
        <div class="main-box" style="text-align: center">
          <span style="margin-left: 50px;"><el-button @click="addTags">确定</el-button></span>
          <span style="margin-left: 50px;"><el-button @click="seecontent = false">取消</el-button></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import msgbox from '../../common/msgbox.js'
import config from '../../common/config'
import MsgList from '../public/msgList'
import editor from '../../assets/editor/editor'
import ImgUpload from '../public/imgUpload'

export default {
  components: {
    ImgUpload,
    MsgList,
    editor: editor
  },
  name: 'questions',
  data() {
    return {
      page: 1,
      addtitle: '新增',
      enabled: 0,
      vtypes: '',
      addvideo: {
        category_id: '',
        qvid: 0,
        title: '',
        author: '',
        enabled: 0,
        year: '',
        view_group: ''
      },
      searchmsg: {
        category_id: '',
        title: '',
        enabled: '',
        year: ''
      },
      yearcates: [
        {
          alias: '全部',
          key: ''
        },
        {
          alias: '2019年',
          key: '2019'
        },
        {
          alias: '2020年',
          key: '2020'
        }
      ],
      btntype: ['修改', '删除'],
      pagesall: 0,
      content: {},
      videocates: [], // 分类数据(搜索)
      videocateadd: [], // 分类数据(新增/修改)
      videocateadd1: [
        {
          alias: 'all',
          key: 'all'
        },
        {
          alias: 'vip',
          key: 'vip'
        }
      ],
      type: true,
      deletes: {
        ids: [],
        reason: ''
      },
      delshow: false,
      searchshow: false,
      seecontent: false,
      title: [
        {
          prop: 'id',
          label: 'id'
        },
        {
          prop: 'title',
          label: '视频标题'
        },
        {
          prop: 'duration',
          label: '时长'
        },
        {
          prop: 'add_time',
          label: '添加时间'
        },
        {
          prop: 'enabled',
          label: '是否可用'
        },
        {
          prop: 'view_group',
          label: '观看权限'
        }
      ],
      tableData: [],
      tid: 0,
      getmsg: {},
      showimg: false,
      videocateaddV: '',
      videocateaddS: '',
      allCate: []
    }
  },
  mounted() {
    this.search()
  },
  watch: {
    'searchmsg.year': {
      handler() {
        this.page = 1
      }
    }
  },
  methods: {
    videocateaddC() {
      console.log(this.videocateaddV)
      this.getC()
    },
    getC() {
      this.allCate = []
      let that = this
      this.$g.ajax({
        path: 'videos/getCategories',
        method: 'post',
        data: {
          videoTopicId: that.videocateaddV
        },
        cbSuccess: function(data) {
          that.allCate = data.memo.children
          that.addvideo.category_id = ''
        },
        cbError: function(data) {
          let msg = msgbox.get(data.msg)
          that.$message.error(msg)
        }
      })
    },
    sureSearch() {
      if (this.videocateaddS !== '' && this.videocateaddS !== 0 && this.searchmsg.category_id === '') {
        this.$message.error('分类不能为空')
        return false
      }
      this.search()
    },
    search() { // 搜索获取数据
      console.log(this.searchmsg)
      let that = this
      this.$g.ajax({
        path: 'videos/getList',
        method: 'post',
        data: {
          title: this.searchmsg.title,
          status: this.searchmsg.status,
          enabled: this.searchmsg.enabled,
          year: this.searchmsg.year,
          category_id: this.searchmsg.category_id,
          page: this.page
        },
        cbSuccess: function(data) {
          that.tableData = data.memo.list
          that.pagesall = parseInt(data.memo.pageTotal)
          that.types()
        },
        cbError: function(data) {
          let msg = msgbox.get(data.msg)
          that.$message.error(msg)
        }
      })
    },
    types() { // 获取分类数据
      let that = this
      this.$g.ajax({
        path: 'videoTopics/getList',
        method: 'post',
        data: {
          page: 1
        },
        cbSuccess: function(data) {
          that.videocateadd = data.memo.list
          that.videocateadd.splice(0, 0, {
            id: 0,
            title: '免费'
          })
        },
        cbError: function(data) {
          let msg = msgbox.get(data.msg)
          that.$message.error(msg)
        }
      })
    },
    addTit(type) {
      let that = this
      if (type === '新增') {
        this.seecontent = true
        this.addtitle = type
        this.addvideo = {
          category_id: '',
          title: '',
          qvid: 0,
          author: '',
          year: '',
          enabled: 0,
          view_group: ''
        }
        this.videocateaddV = ''
        this.enabled = 0
        this.vtypes = 'free'
      } else if (type === '屏蔽' || type === '解除屏蔽') {
        let enabled = ''
        if (type === '屏蔽') {
          enabled = 0
        }
        if (type === '解除屏蔽') {
          enabled = 1
        }
        this.$g.ajax({
          path: 'Videotopics/setStatus/' + enabled,
          method: 'post',
          data: {
            ids: this.deletes.ids
          },
          cbSuccess: function(data) {
            that.search()
          },
          cbError: function(data) {
            let msg = msgbox.get(data.msg)
            that.$message.error(msg)
          }
        })
      }
    },
    addTags() { // 添加/修改视频集
      let that = this
      console.log(that.addvideo)
      that.addvideo.content = this.$refs.editor.getContent()
      if (this.addvideo.title === '') {
        this.$message.error('标题不能为空')
      } else if (this.addvideo.author === '') {
        this.$message.error('作者不能为空')
      } else if (this.addvideo.qvid === 0) {
        this.$message.error('视频ID不能为0')
      } else if (this.addvideo.year === '') {
        this.$message.error('请选择年份')
      } else if (this.videocateaddV === '') {
        this.$message.error('请选择视频主题')
      } else if (this.addvideo.category_id === '' && this.videocateaddV !== 0) {
        this.$message.error('请选择视频分类')
      } else if (this.addvideo.view_group === '') {
        this.$message.error('请选择权限')
      } else {
        if (parseInt(this.enabled) === 0) {
          this.addvideo.enabled = 1
        }
        if (parseInt(this.enabled) === 1) {
          this.addvideo.enabled = 0
        }
        if (this.addtitle === '新增') {
          this.$g.ajax({
            path: 'videos/addVideo',
            method: 'post',
            data: that.addvideo,
            cbSuccess: function(data) {
              that.seecontent = false
              that.addvideo = {
                qvid: 0,
                category_id: '',
                title: '',
                author: '',
                year: '',
                enabled: 0,
                view_group: ''
              }
              that.videocateaddV = ''
              that.$refs.editor.setContent({})
              that.search()
            },
            cbError: function(data) {
              let msg = msgbox.get(data.msg)
              that.$message.error(msg)
            }
          })
        }
        if (this.addtitle === '修改') {
          this.$g.ajax({
            path: 'videos/updateVideo',
            method: 'post',
            data: {
              id: that.tid,
              category_id: that.addvideo.category_id,
              title: that.addvideo.title,
              author: that.addvideo.author,
              enabled: that.addvideo.enabled,
              qvid: that.addvideo.qvid,
              year: that.addvideo.year,
              view_group: that.addvideo.view_group,
              content: that.addvideo.content
            },
            cbSuccess: function(data) {
              that.videocateaddV = ''
              that.$refs.editor.setContent({})
              that.seecontent = false
              that.search()
              that.$message.success('修改成功')
            },
            cbError: function(data) {
              let msg = msgbox.get(data.msg)
              that.$message.error(msg)
            }
          })
        }
      }
    },
    changepage1(page) { // 更改page
      this.page = page
      this.search()
    },
    delhot() { // 删除视频集
      let that = this
      this.$confirm('确定删除视频吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$g.ajax({
          path: 'videos/deleteVideo',
          method: 'post',
          data: {
            ids: [that.tid]
          },
          cbSuccess: function(data) {
            that.search()
          },
          cbError: function(data) {
            let msg = msgbox.get(data.msg)
            that.$message.error(msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getid1(val) { // 获取列表button信息
      console.log(val)
      this.$refs.editor.setContent({})
      let that = this
      if (val.btn === '管理') {
        this.$router.push('/video/total/' + val.val.id)
      }
      if (val.btn === '删除') {
        this.tid = val.val.id
        this.delhot()
      }
      if (val.btn === '修改') {
        that.$g.ajax({
          path: 'videos/getInfo',
          method: 'post',
          data: {
            id: val.val.id
          },
          cbSuccess: function(data) {
            that.getmsg = data.memo
            that.tid = data.memo.id
            that.videocateaddV = data.memo.videoTopic_id ? data.memo.videoTopic_id : 0
            that.seecontent = true
            that.addtitle = '修改'
            that.changemsg()
            that.$g.ajax({
              path: 'videos/getCategories',
              method: 'post',
              data: {
                videoTopicId: that.videocateaddV
              },
              cbSuccess: function(data) {
                that.allCate = data.memo.children
              },
              cbError: function(data) {
                let msg = msgbox.get(data.msg)
                that.$message.error(msg)
              }
            })
          },
          cbError: function(data) {
            let msg = msgbox.get(data.msg)
            that.$message.error(msg)
          }
        })
      }
      if (val.btn === '预览') {
        this.getmsg = val.val
        this.showimg = true
      }
    },
    getselect(vals) { // 获取多选id
      console.log(vals)
      this.deletes.ids = []
      for (let i in vals) {
        this.deletes.ids.push(vals[i].id)
      }
    },
    changemsg() {
      this.addvideo = this.getmsg
      this.$refs.editor.setContent(this.addvideo.content)
      if (this.getmsg.enabled === 1) {
        this.enabled = 0
      }
      if (this.getmsg.enabled === 0) {
        this.enabled = 1
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.active {
  display: inline-block;
  -moz-transform: rotate(180deg);
  -webkit-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}

#videolist {
  .main-box {
    margin-top: 20px;

    span {
      margin-right: 30px;
      font-size: 14px;
      font-weight: 700;
      color: #606266;
    }

    .el-input {
      width: 240px;
    }
  }

  .main-box {
    .el-radio__inner {
      background: red;
    }

    .el-radio__input.is-checked + .el-radio__label {
      color: #00a4a0 !important;
    }
  }

  .layer {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 999;
    overflow: hidden;

    .mask {
      background: rgba(153, 153, 153, 0.5);
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      z-index: 1;
    }

    .imgcontent {
      text-align: center;

      img {
        position: relative;
        max-width: 80%;
        max-height: 80%;
        background: #fff;
        margin: 5% auto;
        z-index: 3;
      }
    }

    .content {
      position: relative;
      width: 60%;
      max-height: 80%;
      background: #fff;
      margin: 5% auto;
      z-index: 2;
      padding: 30px;
      overflow: scroll;

      .close {
        position: absolute;
        right: 10px;
        top: 10px;
        cursor: pointer;
      }

      .inputcl {
        margin-top: 20px;

        .layer-list-title {
          display: inline-block;
          width: 80px;
          margin-left: 15px;
          margin-right: 10px;
          float: left;
          vertical-align: middle;
          line-height: 28px;
        }

        .img-list {
          vertical-align: top;
          float: left;
          line-height: 18px;
        }

        .imgupload {
          display: block;
          margin-left: 105px;
        }

        .el-input {
          max-width: 200px;
        }

        img {
          width: 320px;
          height: 240px;
        }
      }

      .title {
        text-align: center;
        font-size: 18px;
        font-weight: bold;
      }

      .doit .one .el-input {
        width: 200px;
        margin-left: 20px;
      }

      .doit .one {
        margin-top: 20px;
        font-size: 14px;
      }

      .el-date-editor.el-input, .el-date-editor.el-input__inner {
        position: relative;
        left: -4px;
      }

      .buttons {
        margin-top: 20px;
        text-align: center;

        button {
          margin: 0 20px;
        }
      }
    }
  }
}

.main-cctv {
  min-height: 200px;
  border: 1px solid #cccccc;
}
</style>
<style>
.el-button:focus, .el-button:hover {
  color: #00a4a0;
}

.el-button:active {
  border-color: #00a0a0;
}

.el-button {
  padding: 8px 14px;
}

.el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background: #00a4a0;
  border-color: #00a4a0;
}

.transition-box {
  margin-bottom: 10px;
  width: 200px;
  height: 100px;
  border-radius: 4px;
  background-color: #409EFF;
  text-align: center;
  color: #fff;
  padding: 40px 20px;
  box-sizing: border-box;
  margin-right: 20px;
}

#videolist .el-textarea__inner {
  resize: none;
}
</style>
